package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberMapper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.dao.SetmealMapper;
import com.itheima.health.entity.OrderInfo;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@DubboService
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Transactional
    @Override
    public Order add(Order order) {
        log.info("[预约]data:{}",order);
        //前端拿来的对象 去掉时分秒 去数据库查询
        //构建日历对象
        Calendar calendar= Calendar.getInstance();
        //设置前端传来的时间
        calendar.setTime(order.getOrderDate());
        //calendar.get(Calendar.YEAR)获取前端时间传来的年    时分秒设置为零
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),0,0,0);
        log.info("处理以后的日期"+calendar.getTime());
        //根据日期查询是否存在和已经预约人数
        OrderSetting orderSetting = new OrderSetting();
        orderSetting.setOrderDate(calendar.getTime());
        //查询返回的order对象
        OrderSetting orderSetting1 = orderSettingMapper.selectOne(orderSetting);
        if(null==orderSetting1){
            throw new BusinessException(MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        if(orderSetting1.getReservations()>=orderSetting1.getNumber()){
            throw new BusinessException(MessageConst.ORDER_FULL);
        }
        //查询order表 是否存在同一天同一个人同一个套餐  重复预约
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("memberId", order.getMemberId());
        criteria.andEqualTo("orderDate", calendar.getTime());
        criteria.andEqualTo("setmealId", order.getSetmealId());
        Order order1 = orderMapper.selectOneByExample(example);
        if(null!=order1){
            throw new BusinessException(MessageConst.HAS_ORDERED);
        }
        //如果都没有且判断通过 则插入
        orderMapper.insertSelective(order);
        //修改已预约数  （上边已经）查询以后做更新
        orderSetting1.setId(orderSetting1.getId());
        orderSetting1.setReservations(orderSetting1.getReservations()+1);
        orderSettingMapper.updateByPrimaryKeySelective(orderSetting1);

        return orderMapper.selectByPrimaryKey(order.getId());
    }

    /**
     * @Author: JASON
     * @Date: 9:27 2021/1/11
     * @Parms [id]
     * @ReturnType: com.itheima.health.pojo.Order
     * @Description: 根据id查找订单
    */
    @Override
    public OrderInfo findById(Integer id) {
        OrderInfo orderInfo = new OrderInfo();
        Order order = orderMapper.selectByPrimaryKey(id);
        //根据memberid查member
        Member member = memberMapper.selectByPrimaryKey(order.getMemberId());
        orderInfo.setMember(member.getName());
        //根据setmealid 查setmeal
        Setmeal setmeal = setmealMapper.selectByPrimaryKey(order.getSetmealId());
        orderInfo.setSetmeal(setmeal.getName());
        orderInfo.setOrderType(order.getOrderType());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        orderInfo.setOrderDate(sdf.format(order.getOrderDate()));
        return orderInfo;
    }

    @Override
    public List<Map<String, Object>> countBySetMeal() {

        List<Map<String, Object>> list = orderMapper.countBySetMeal();
        return list;
    }

}
